package com.zf.shuashua.ui.mine.notice;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

import com.zf.shuashua.R;
import com.zf.shuashua.api.AbsCallback;
import com.zf.shuashua.api.HttpData;
import com.zf.shuashua.base.BaseActivity1;
import com.zf.shuashua.bean.BaseData;
import com.zf.shuashua.bean.NotificationInfo;
import com.zf.shuashua.ui.mine.MineFragment;
import com.zf.shuashua.utils.CommonConstants;

/**
 * 消息通知首页
 */
public class MessageActivity extends BaseActivity1 implements View.OnClickListener {
    private Toolbar mToolbar;
    private TextView mTvTitle, mTvComment, mTvLove, mTvFeed, mTvSystem, mTvNotice;

    @Override
    protected void initView() {
        mToolbar = findViewById(R.id.toolbar);
        mTvTitle = findViewById(R.id.tv_title);
        initToolBar(mToolbar, true, "");
        mTvTitle.setText("消息");
        mTvComment = findViewById(R.id.tv_comment_number);
        mTvLove = findViewById(R.id.tv_love_number);
        mTvFeed = findViewById(R.id.tv_feed_number);
        mTvSystem = findViewById(R.id.tv_system_number);
        mTvNotice = findViewById(R.id.tv_notice_title);

        findViewById(R.id.ll_comment).setOnClickListener(this);
        findViewById(R.id.ll_love).setOnClickListener(this);
        findViewById(R.id.ll_feed).setOnClickListener(this);
        findViewById(R.id.ll_system).setOnClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_message;
    }

    @Override
    protected void initData() {
        super.initData();

    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData();
    }

    private void loadData() {
        HttpData.getInstance().getNotificationInfo(new AbsCallback<BaseData<NotificationInfo>>() {

            @Override
            public void onRealSuccess(BaseData<NotificationInfo> data) {
                if (data != null && data.isStatus()) {
                    setData(data.getData());
                }
            }

            @Override
            public void onFail(int errCode, String errMsg) {

            }
        });
    }

    private void setData(NotificationInfo info) {
        if (info == null) {
            return;
        }
        if (info.getFormatCount(info.un_read_comment_count) > 0) {
            mTvComment.setVisibility(View.VISIBLE);
            mTvComment.setText(info.un_read_comment_count);
        } else {
            mTvComment.setVisibility(View.GONE);
        }
        if (info.getFormatCount(info.un_read_praise_count) > 0) {
            mTvLove.setVisibility(View.VISIBLE);
            mTvLove.setText(info.un_read_praise_count);
        } else {
            mTvLove.setVisibility(View.GONE);
        }
        if (info.getFormatCount(info.un_read_follow_count) > 0) {
            mTvFeed.setVisibility(View.VISIBLE);
            mTvFeed.setText(info.un_read_follow_count);
        } else {
            mTvFeed.setVisibility(View.GONE);
        }
        if (info.getFormatCount(info.un_read_notice_count) > 0) {
            mTvSystem.setVisibility(View.VISIBLE);
            mTvSystem.setText(info.un_read_notice_count);
        } else {
            mTvSystem.setVisibility(View.GONE);
        }
        String message = TextUtils.isEmpty(info.notice_title) ? "您还没有消息" : info.notice_title;
        mTvNotice.setText(message);
    }

    private String getFormatCount(int count) {
        if (count > 99) {
            return "99+";
        }
        return count + "";
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CommonConstants.XITONGXIAOXI) {
            if (resultCode == RESULT_OK) {
                setResult(RESULT_OK);
                finish();
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_comment:
                startActivity(new Intent(this, CommentActivity.class));
                break;
            case R.id.ll_love:
                startActivity(new Intent(this, LoveActivity.class));
                break;
            case R.id.ll_feed:
                startActivity(new Intent(this, FeedActivity.class));
                break;
            case R.id.ll_system:
                startActivityForResult(new Intent(this, NoticeActivity.class), CommonConstants.XITONGXIAOXI);
                break;
        }
    }
}
